<?php
class BZ_Model extends CI_Model
{
    protected $table;
    
    public function findOne($params = [], $field = '*', $order = null)
    {
        $this->db->select($field);
        $params = $this->validateParams($params);
        $this->db->where($params);
        $this->db->from($this->table);
        if ($order !== null) {
            $this->db->order_by($order);
        }
        return $this->db->get();
    }

    public function findAll($params = [], $field = '*', $isArray = false)
    {
        $this->db->select($field);
        $params = $this->validateParams($params);
        $this->db->where($params);
        $this->db->from($this->table);
        $result = $this->db->get();
        if ($isArray) {
            $row = [];
            foreach($result->result_array() as $key => $item) {
                $row[$item['id']] = $item;
            }
            return $row;
        }
        return $result;
    }

    public function findByParams($params = [], $field = '*', $limit = null, $order = null)
    {
        $this->db->select($field);
        $params = $this->validateParams($params);
        $this->db->where($params);
        $this->db->from($this->table);
        if ($order !== null) {
            $this->db->order_by($order);
        }
        if ($limit !== null) {
            $this->db->limit($limit);
        }
        return $this->db->get();;
    }

    public function findByCount($params = [])
    {
        $params = $this->validateParams($params);
        $this->db->where($params);
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    public function findPageList($num, $pgNum, $params = [], $field = '*', $order = null)
    {
        $this->db->select($field);
        $params = $this->validateParams($params);
        $this->db->where($params);
        $this->db->from($this->table);
        $this->db->limit($pgNum, $num);
        if ($order !== null) {
            $this->db->order_by($order);
        }
        return $this->db->get();
    }

    public function insert($params = [])
    {
        if (empty($params)) {
            return false;
        }
        $this->db->insert($this->table, $params);
        return $this->db->insert_id();
    }

    public function update($params = [], $where = [])
    {
        if (empty($where) || empty($params)) {
           return false;
        }
        $params = $this->validateParams($params);
        $this->db->where($where);
        return $this->db->update($this->table, $params);
    }

    public function delete($where = [])
    {
        if (empty($where)) {
            return false;
        }
        $where = $this->validateParams($where);
        $this->db->where($where);
        return $this->db->delete($this->table);
    }

    //['id' => ['in' => [1, 2, 3]]]
    private function validateParams($params)
    {
        foreach ($params as $key => $val) {
            if (!empty($val['in'])) {
                $this->db->where_in($key, $val['in']);
                unset($params[$key]);
            }
        }
        return $params;
    }
}